package org.jackysoft.entity;

import java.util.Collection;



import org.jackysoft.annotations.Transient;
import org.jackysoft.bean.define.EntitySchemas;
import org.jackysoft.entity.base.AbstractEntity;
import org.jackysoft.util.NoSerialize;



public class TreeNode extends AbstractEntity implements Comparable<TreeNode> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String type;
	private String description;
	private String parentId;
	private String identifier;
	private String hrefTarget;
	private boolean visiable = true;
	private int contextType;
	private Collection<SecurityRole> permissions;

	public TreeNode() {
	}

	public TreeNode(String id, String description) {
		this.id = id;
		this.description = description;
	}

	public TreeNode(String id, String description, String identifier,
			String parentId) {
		this.id = id;
		this.description = description;
		this.parentId = parentId;
		this.identifier = identifier;
	}

	public TreeNode(String id, String identifier, String description,
			boolean leaf, String parentId, String type, int sort) {
		this.sort = sort;
		this.id = id;
		this.description = description;
		this.parentId = parentId;
		this.identifier = identifier;
		this.type = type;
		this.leaf = leaf;
	}
	public TreeNode(String id, String identifier, String description,
			boolean leaf, String parentId, String type, int sort,int contextType) {
		 this(id,identifier,description,leaf,parentId,type,sort);
		 this.contextType = contextType;
	}

	public TreeNode(String id, String identifier, String description,
			boolean leaf, String parentId, String type, int sortedNum,
			boolean visiable) {
		this.sort = sortedNum;
		this.id = id;
		this.description = description;
		this.parentId = parentId;
		this.identifier = identifier;
		this.type = type;
		this.leaf = leaf;
		this.visiable = visiable;		

	}
	public TreeNode(String id, String identifier, String description,
			boolean leaf, String parentId, String type, int sort,
			boolean visiable,int contextType) {
		this(id,identifier,description,leaf,parentId,type,sort,visiable);
		this.contextType = contextType;
		
	}

	/**
	 * 
	 * a.id ,a.description * ,a.identifier ,a.leaf ,a.parentId ,a.sortedNum
	 * ,a.type
	 */
	public TreeNode(String id, String des, String identifier, Boolean isleaf,
			String parentId, Integer sorted, String type) {
		setId(id);
		setParentId(parentId);
		setDescription(des);
		setType(type);
		setLeaf(isleaf);
		setIdentifier(identifier);

	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public boolean isLeaf() {
		this.leaf = "document".equals(getType());
		return this.leaf;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getType() {
		return type;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	
	@NoSerialize
	
	public Collection<SecurityRole> getPermissions() {
		return permissions;
	}

	public void setPermissions(Collection<SecurityRole> permissions) {
		this.permissions = permissions;
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj || !(obj instanceof TreeNode) || id == null)
			return false;
		TreeNode o = (TreeNode) obj;
		if (o.getId() == null)
			return false;
		return o.getId().equals(this.getId());
	}

	@Override
	public int compareTo(TreeNode o) {
		if (!(o instanceof TreeNode) || o == null)
			return -1;
		if (this.sort > o.sort)
			return 1;
		else if (this.sort == o.sort)
			return 0;
		else if (this.sort < o.sort)
			return -1;
		else
			return 0;
	}

	public boolean isVisiable() {
		return visiable;
	}

	public void setVisiable(boolean visiable) {
		this.visiable = visiable;
	}

	public String getHrefTarget() {
		return hrefTarget;
	}

	public void setHrefTarget(String hrefTarget) {
		this.hrefTarget = hrefTarget;
	}

	public void setText(String text) {
		this.setDescription(text);
	}

	public String getText() {
		return getDescription();
	}
	public void setUrl(String name){
		this.identifier = name;
	}
	
	@Transient
	public String getUrl(){
		return this.identifier;
	}	

	public int getContextType() {
		return contextType;
	}

	public void setContextType(int contextType) {
		this.contextType = contextType;
	}
}
